package com.crm.service.system.impl;

import com.crm.context.BaseContext;
import com.crm.dto.DeptDto;
import com.crm.exception.BaseException;
import com.crm.mapper.system.DeptMapper;
import com.crm.pojo.Dept;
import com.crm.service.system.DeptService;
import com.crm.vo.TreeselectVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName DeptServiceImpl
 * @Description
 * @Author 19632
 * @Date 2024/10/29 11:39
 * @Version
 **/
@Service
@Slf4j
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list(String status, String deptName) {

        return deptMapper.list(status,deptName);
    }

    @Override
    public void add(Dept dept) {
        log.info("接收参数为:{}",dept);
        Integer parentId = dept.getParentId();
        //查父类的数据
        Dept deptn=deptMapper.searchAll(parentId);
        Integer deptId = deptn.getDeptId();
        String ancestors = deptn.getAncestors();
        List<Character> digits = new ArrayList<>();
        for (char c = '1'; c <= '9'; c++) { // 注意从'1'开始，避免生成的三位数以'0'开头
            digits.add(c);
        }

        // 打乱列表顺序
        Collections.shuffle(digits);

        // 取前三个字符作为结果
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 3; i++) {
            sb.append(digits.get(i));
        }
        String deptIdNew = sb.toString();

        StringBuilder ass = new StringBuilder(ancestors);
        ass.append(",");
        ass.append(deptId);

        String ancestorsNew = ass.toString();

        Dept deptNew = new Dept();
        BeanUtils.copyProperties(dept,deptNew);
        deptNew.setAncestors(ancestorsNew);
        deptNew.setDeptId(Integer.valueOf(deptIdNew));
        String currentUserName = BaseContext.getCurrentUserName();
        deptNew.setCreateBy(currentUserName);

        deptMapper.add(deptNew);

    }

    @Override
    public Dept getByDeptId(Integer deptId) {
        return deptMapper.getByDeptId(deptId);
    }

    @Override
    public List<Dept> getByDeptIdlist(Integer deptId) {
        return deptMapper.getByDeptIdlist();
    }

    @Override
    public void update(Dept dept) {
        log.info("接收参数为:{}",dept);

        Integer parentId = dept.getParentId();
        //查父类
        Dept byDeptId1 = deptMapper.getByDeptId(parentId);
        String ancestors = byDeptId1.getAncestors();
        Integer deptId1 = byDeptId1.getDeptId();
        StringBuilder ass = new StringBuilder(ancestors);
        ass.append(",");
        ass.append(deptId1);

        dept.setAncestors(String.valueOf(ass));
        dept.setParentId(deptId1);
        dept.setUpdateBy(BaseContext.getCurrentUserName());
        deptMapper.update(dept);
    }

    @Override
    public void delete(Integer deptId) {
        List<Dept>deptList=deptMapper.getDeptId(deptId);
        if(!CollectionUtils.isEmpty(deptList)){
            throw new BaseException("此阶层下有数据,不可删除!!");
        }
        deptMapper.delete(deptId);
    }


    @Override
    public List<TreeselectVO> getTree() {
        List<TreeselectVO> deptList = deptMapper.selectAllDepts();
        List<TreeselectVO> list = treeSelectVOList(deptList);
        return list;
    }

    private List<TreeselectVO> treeSelectVOList(List<TreeselectVO> deptList) {
        List<TreeselectVO> result = new ArrayList<>();
        deptList.forEach(d -> {
            if (d != null && d.getParentId() == 0) {
                result.add(findChildren(d, deptList));
            }
        });
        return result;
    }

    private TreeselectVO findChildren(TreeselectVO parent, List<TreeselectVO> deptList) {
        List<TreeselectVO> children = new ArrayList<>();
        deptList.forEach(d -> {
            if (d != null && parent.getId().equals(d.getParentId())) {
                children.add(findChildren(d, deptList));
            }
        });
        parent.setChildren(children);
        return parent;
    }
}
